package com.tigercel.xiangchengyun.utils

import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject

/**
 *
 * Created by wqj on 2018/5/24.
 */
class JsonUtil {
    companion object {


        fun get_key_string(key: String, jsonString: String): String {
            var str: String = ""

            try {

                val jsonObj: JSONObject = JSONObject(jsonString)
                str = jsonObj.optString(key)


            } catch (e: JSONException) {
                e.printStackTrace()
            }
            return str
        }

        fun get_key_boolean(key: String, jsonString: String): Boolean {
            var str: Boolean = true
            try {
                val jsonObj: JSONObject = JSONObject(jsonString)
                str = jsonObj.optBoolean(key)
            } catch (e: JSONException) {
                e.printStackTrace()
            }
            return str
        }

        fun get_key_int(key: String, jsonString: String): Int {
            var str: Int = 0
            try {
                val jsonObj: JSONObject = JSONObject(jsonString)
                str = jsonObj.optInt(key)
            } catch (e: JSONException) {
                e.printStackTrace()
            }
            return str
        }


        fun getList(key: String, jsonString: String): List<String> {
            val list = ArrayList<String>()
            try {
                val jsonObject = JSONObject(jsonString)
                val jsonArray: JSONArray?
                try {
                    jsonArray = jsonObject.optJSONArray(key)
                    for (i in 0..jsonArray.length() - 1) {
                        val msg = jsonArray.optString(i)
                        list.add(msg)
                    }
                } catch (e: Exception) {
                }


            } catch (e: JSONException) {
                e.printStackTrace()
            }

            return list
        }


        fun getListMap(key: String, jsonString: String): List<Map<String, Any>> {
            val list = ArrayList<Map<String, Any>>()
            try {
                val jsonObject = JSONObject(jsonString)
                val jsonArray = jsonObject.optJSONArray(key)
                for (i in 0..jsonArray.length() - 1) {
                    val jsonObject2 = jsonArray.optJSONObject(i)
                    val map = HashMap<String, Any>()
                    val iterator = jsonObject2.keys()
                    while (iterator.hasNext()) {
                        val json_key = iterator.next()
                        var json_value: Any? = jsonObject2.opt(json_key)
                        if (json_value == null) {
                            json_value = ""
                        }
                        map.put(json_key, json_value)
                    }
                    list.add(map)
                }
            } catch (e: JSONException) {

                e.printStackTrace()
            }
            return list
        }


//        fun get_ask_Item(key:String,jsonString:String): ArrayList<MyAsk>? {
//            var myAsk=ArrayList<MyAsk>()
//            try{
//                val jsonObject=JSONObject(jsonString)
//                val jsonArray=jsonObject.getJSONArray(key)
//                for(i in 0..jsonArray.length()-1){
//                    val json_ask=jsonArray.getJSONObject(i)
//                    var my_ask=MyAsk()
//                    val commentCount=json_ask.getString("commentCount")
//                    val commentID=json_ask.getString("commentID")
//                    val content=json_ask.getString("content")
//                    val createTime=json_ask.getString("createTime")
//                    val gender=json_ask.getString("gender")
//                    val icon=json_ask.getString("icon")
//                    val id=json_ask.getString("id")
//                    val isFavorite=json_ask.getString("isFavorite")
//                    val isZan=json_ask.getString("isZan")
//                    val label=json_ask.getString("label")
//                    val link=json_ask.getString("link")
//                    val nickname=json_ask.getString("nickname")
//                    val publisherID=json_ask.getString("publisherID")
//                    val reward=json_ask.getString("reward")
//                    val type=json_ask.getString("type")
//                    val zan=json_ask.getString("zan")
//
//                    val image_array=json_ask.getJSONArray("image")
//                    var image_list:ArrayList<String>?=null
//                    for(j1 in 0..image_array.length()-1){
//                        val image_item=image_array.getString(j1)
//                        image_list?.add(image_item)
//                    }
//
//                    val thumbnailImage_array=json_ask.getJSONArray("thumbnailImage")
//                    var image_list2:ArrayList<String>?=null
//                    for(j2 in 0..thumbnailImage_array.length()-1){
//                        val image_item=thumbnailImage_array.getString(j2)
//                        image_list2?.add(image_item)
//                    }
//
//                    my_ask.commentID=commentID
//                    my_ask.commentCount=commentCount
//                    my_ask.content=content
//                    my_ask.createTime=createTime
//                    my_ask.gender=gender
//                    my_ask.icon=icon
//                    my_ask.id=id
//                    my_ask.favoriteIs=isFavorite
//                    my_ask.zan=zan
//                    my_ask.zanIs=isZan
//                    my_ask.label=label
//                    my_ask.link=link
//                    my_ask.nickname=nickname
//                    my_ask.publisherID=publisherID
//                    my_ask.reward=reward
//                    my_ask.type=type
//                    my_ask.image=image_list
//                    my_ask.thumbnailImage=image_list2
//
//                    myAsk?.add(my_ask)
//                }
//                return myAsk
//            }catch (e: JSONException){
//                e.printStackTrace()
//            }
//            return myAsk
//        }

    }

}